package io.gitee.zhangbinhub.acp.boot.tools

import io.gitee.zhangbinhub.acp.core.common.CommonTools
import io.gitee.zhangbinhub.acp.core.common.NetTools
import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.server.ServerWebExchange

/**
 * IP 相关工具类
 */
object IpTools {
    /**
     * 获取WEB应用访问服务器的IP
     *
     * @return 服务器IP
     */
    @JvmStatic
    fun getServerIps(): List<String> = NetTools.getServerIps()

    /**
     * 获取WEB应用访问服务器的IP
     *
     * @return 服务器IP
     */
    @JvmStatic
    fun getServerIp(): String? = NetTools.getServerIp()

    /**
     * 获取WEB应用访问服务器的物理地址
     *
     * @return 服务器物理地址
     */
    @JvmStatic
    fun getMacAddress(): String = NetTools.getMacAddress()

    /**
     * 获取远程客户端IP
     *
     * @param request 请求对象
     * @return IP
     */
    @JvmStatic
    fun getRemoteIP(request: HttpServletRequest): String? {
        var ipAddress: String? = request.getHeader("X-Forwarded-For")
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = request.getHeader("Citrix-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = request.getHeader("Proxy-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = getRemoteRealIP(request)
        }
        return NetTools.formatIpAddress(ipAddress)
    }

    /**
     * 获取远程客户端IP
     *
     * @param exchange 请求对象
     * @return IP
     */
    @JvmStatic
    fun getRemoteIP(exchange: ServerWebExchange): String? = exchange.request.headers.let { httpHeaders ->
        var ipAddress: String? = httpHeaders.getFirst("X-Forwarded-For")
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = httpHeaders.getFirst("Citrix-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = httpHeaders.getFirst("Proxy-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = httpHeaders.getFirst("WL-Proxy-Client-IP")
        }
        if (CommonTools.isNullStr(ipAddress) || "unknown".equals(ipAddress!!, ignoreCase = true)) {
            ipAddress = getRemoteRealIP(exchange)
        }
        NetTools.formatIpAddress(ipAddress)
    }

    /**
     * 获取远程客户端IP
     *
     * @param session websocket会话
     * @return IP
     */
    @JvmStatic
    fun getRemoteIP(session: org.springframework.web.socket.WebSocketSession): String? =
        NetTools.getIpFromInetAddresses(session.remoteAddress?.address)

    /**
     * 获取远程客户端IP
     *
     * @param session websocket会话
     * @return IP
     */
    @JvmStatic
    fun getRemoteIP(session: org.springframework.web.reactive.socket.WebSocketSession): String? =
        NetTools.getIpFromInetAddresses(session.handshakeInfo.remoteAddress?.address)

    /**
     * 获取远程客户端真实底层IP
     *
     * @param request 请求对象
     * @return IP
     */
    private fun getRemoteRealIP(request: HttpServletRequest): String? = request.remoteAddr

    /**
     * 获取远程客户端真实底层IP
     *
     * @param exchange 请求对象
     * @return IP
     */
    private fun getRemoteRealIP(exchange: ServerWebExchange): String? =
        NetTools.getIpFromInetAddresses(exchange.request.remoteAddress?.address)
}
